import waterfall from 'async/waterfall'
import isArray from 'lodash/isArray'
import trim from 'lodash/trim'
import { getDataByPath, findListItem } from '@ve/utils/utils'

//必须带done回调 执行完成调用
export default function clickTableRow({ vm, action, done, callbackData, additionalData }) {
  let tableIds = action.extension.tableId.value
  console.log('单击 tableIds', tableIds, action)
  let _task = []
  isArray(tableIds) &&
    tableIds.forEach((tableId) => {
      let _rowNumber = 0
      if (tableId) {
        let _table = vm.vuiComponents[tableId]
        //new code
        if (action.extension.selectRowType) {
          let selectRowType = action.extension.selectRowType.value
          if (selectRowType.type === '1') {
            //1固定下标
            _rowNumber = selectRowType.index
          } else if (selectRowType.type === '2') {
            //2动态获取
            let _rowData = {}
            if (selectRowType.key.indexOf(',') > -1) {
              // 如果有逗号拼接则为多条件匹配
              const keyList = selectRowType.key.split(',')
              _table.ref.getData().forEach((v) => {
                let flag = true
                keyList.forEach((keyItem) => {
                  const val = getDataByPath(v, trim(keyItem))
                  const callbackVal = getDataByPath(callbackData, trim(keyItem))
                  // 如果有其中一个字段没匹配上则匹配失败
                  if (val != callbackVal) {
                    flag = false
                  }
                })
                if (flag) {
                  _rowData = v
                }
              })
            } else {
              // let _value = callbackData[selectRowType.key]
              const _value = getDataByPath(callbackData, selectRowType.key) //获取上个任务返回的值
              _table.ref.getData().forEach((v) => {
                const val = getDataByPath(v, selectRowType.key)
                if (val == _value) {
                  _rowData = v
                }
              })
            }
            console.log('获取到的行', _rowData, callbackData, getDataByPath(callbackData, selectRowType.key))

            _rowNumber = _table.ref.getRowIndex(_rowData)
          } else {
            let _rowData = additionalData || {}
            const tableData = _table.ref.getData()
            console.log('主键字段', _table.self, additionalData, tableData)
            if (_table.self.getPrimaryKey) {
              // 如果有主键字段 则用主键字段匹配
              _rowData = findListItem(_rowData, _table.self.getPrimaryKey, tableData)
            }
            _rowNumber = _table.ref.getRowIndex(_rowData)
          }
        } else {
          //兼容旧配置
          _rowNumber = action.extension.rowNumber ? action.extension.rowNumber.value : 0
        }

        console.log('单击 tableIds-1', _table)
        if (_table) {
          // 写入下标偏移量
          if (action.extension.indexOffset && action.extension.indexOffset.value) _rowNumber += action.extension.indexOffset.value

          let _info = _table.ref.getData(parseInt(_rowNumber))
          if (_info) {
            _task.push((cb) => {
              _table.self.cellClickEvent({
                row: _info,
              })
              setTimeout(() => {
                cb()
              }, 10)
            })
          } else {
            console.log('没有找到对应row')
          }
        } else {
          console.log('手动触发表格点击-没有找到表格')
        }
      }
    })

  waterfall(_task, function (err, result) {
    console.log('完成 clickTableRow', err, result)
    done(null, {})
  })
}
